package com.cssl.controller;


import com.cssl.domain.CategorySellAmts;
import com.cssl.domain.DayStatis;
import com.cssl.domain.vo.CategorySellVo;
import com.cssl.domain.vo.OrderAmtsVo;
import com.cssl.domain.vo.UserOrderCntVo;
import com.cssl.service.DtsChartsService;

import com.cssl.service.StatsService;
import com.cssl.util.InfoCode;
import com.cssl.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/charts")
public class DtsChartsController {
    @Autowired
    private DtsChartsService chartsService;
    @Autowired
    private StatsService statsService;
    private static final int STATIS_DAYS_RANG = 30;// 统计的天数范围，一个月数据

    @PostMapping("/info")
    public Result info() {

        Map data = new HashMap();
        data.put("userTotal", chartsService.userTotal());
        data.put("goodsTotal", chartsService.goodsTotal());
        data.put("productTotal", chartsService.productTotal());
        data.put("orderTotal", chartsService.orderTotal());
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, data, System.currentTimeMillis());

    }

    @PostMapping("/chart")
    public Result chart() {
        // 近期用户，订单增长量查询
        UserOrderCntVo userOrderCnt = new UserOrderCntVo();
        List<DayStatis> userCnts = statsService.statisIncreaseUserCnt(STATIS_DAYS_RANG);
        System.out.println("userCnts:"+userCnts);
        List<DayStatis> orderCnts = statsService.statisIncreaseOrderCnt(STATIS_DAYS_RANG);
        System.out.println("orderCnts:"+orderCnts);
        String[] dayData = unionDayData(userCnts, orderCnts);
        userOrderCnt.setDayData(dayData);
        userOrderCnt.setUserCnt(fetchArrCnt(dayData, userCnts));
        userOrderCnt.setOrderCnt(fetchArrCnt(dayData, orderCnts));
        //订单请款统计，订单笔数与订单金额
        OrderAmtsVo orderAmts = fetchOrderAmtsVo(orderCnts);
// 大类销售统计情况
        List<CategorySellAmts> categorySellStatis = statsService.categorySell();// 统计总量
        CategorySellVo categorySell = fetchCategorySell(categorySellStatis);

        Map data = new HashMap<>();
        data.put("userOrderCnt", userOrderCnt);
        data.put("orderAmts", orderAmts);
        data.put("categorySell", categorySell);
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, data, System.currentTimeMillis());
    }

    /**
     * 获取日期数据并排序
     *
     * @param userCnts
     * @param orderCnts
     * @return
     */
    private String[] unionDayData(List<DayStatis> userCnts, List<DayStatis> orderCnts) {
        Set<String> days = new HashSet<>();
        for (DayStatis userCnt : userCnts) {
            days.add(userCnt.getDayStr());
        }
        for (DayStatis orderCnt : orderCnts) {
            days.add(orderCnt.getDayStr());
        }

		/*days.stream().sorted(Comparator.reverseOrder());// 排序
		return days.toArray(new String[days.size()]);*/

        List<String> list = new ArrayList<String>(days);
        Collections.sort(list);
        return list.toArray(new String[0]);
    }

    /**
     * 从统计集合中获取数量 不存在则设置 0
     *
     * @param dayData
     * @param dayStatisCnts
     * @return
     */
    private int[] fetchArrCnt(String[] dayData, List<DayStatis> dayStatisCnts) {
        int[] arrCnts = new int[dayData.length];
        for (int i = 0; i < dayData.length; i++) {
            int dayCnt = 0;
            String dayStr = dayData[i];
            for (DayStatis ds : dayStatisCnts) {
                if (dayStr.equals(ds.getDayStr())) {
                    dayCnt = ds.getCnts();
                    break;
                }
            }
            arrCnts[i] = dayCnt;
        }
        return arrCnts;
    }

    /**
     * 获取订单统计数据
     *
     * @param orderCnts
     * @return
     */
    private OrderAmtsVo fetchOrderAmtsVo(List<DayStatis> orderCnts) {
        OrderAmtsVo orderAmts = new OrderAmtsVo();
        int size = 0;
        if (orderCnts != null && orderCnts.size() > 0) {
            size = orderCnts.size();
        }
        String[] dayData = new String[size];
        int[] orderCntData = new int[size];
        BigDecimal[] orderAmtData = new BigDecimal[size];
        for (int i = 0; i < size; i++) {
            dayData[i] = orderCnts.get(i).getDayStr();
            orderCntData[i] = orderCnts.get(i).getCnts();
            orderAmtData[i] = orderCnts.get(i).getAmts();
        }
        orderAmts.setDayData(dayData);
        orderAmts.setOrderAmtData(orderAmtData);
        orderAmts.setOrderCntData(orderCntData);
        return orderAmts;
    }

    /**
     * 获取大类的销售统计数据
     * <p>
     * //     * @param categorySellStatis
     *
     * @return
     */
    private CategorySellVo fetchCategorySell(List<CategorySellAmts> categorySellData) {
        CategorySellVo categorySell = new CategorySellVo();
        int size = 0;
        if (categorySellData != null && categorySellData.size() > 0) {
            size = categorySellData.size();
        }
        String[] categoryNames = new String[size];
        for (int i = 0; i < size; i++) {
            categoryNames[i] = categorySellData.get(i).getName();
        }
        categorySell.setCategoryNames(categoryNames);
        categorySell.setCategorySellData(categorySellData);
        return categorySell;
    }
}
